﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Autofac;
using FluentNHibernate.Cfg;
using Noodles.Core.DBSettings;
using Noodles.Core.DBSettings.DBProviders;
using Noodles.Core.Model;
using Noodles.Core.Repositories;
using Noodles.Core.Settings;
using NHibernate;
using NHibernate.Bytecode;
using FluentNHibernate.Cfg.Db;
using System.Web.Hosting;
using System.Data.SqlServerCe;
using Noodles.Core.Model.Mappings;
using System.Reflection;

namespace Noodles.Core.Repositories
{
    public class RepositoriesModule :Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {        
            builder.RegisterType<AdminRepository>().As<IAdminRepository>().InstancePerLifetimeScope();         
            builder.RegisterType<LoginRepository>().As<ILoginRepository>().InstancePerLifetimeScope();
            builder.RegisterType<NHibernateRepository>().As<IRepository>().InstancePerLifetimeScope();
            builder.RegisterType<NoodlesRepository>().As<INoodlesRepository>().InstancePerLifetimeScope();
            builder.RegisterType<BlogRepository>().As<IBlogRepository>().InstancePerLifetimeScope();

            builder.Register(ConfigureSessionFactory).As<ISessionFactory>().SingleInstance();
            builder.Register(c => c.Resolve<ISessionFactory>().OpenSession()).As<ISession>().InstancePerLifetimeScope();
        }

        private static ISessionFactory ConfigureSessionFactory(IComponentContext context)
        {       
                string dbPath = HostingEnvironment.MapPath("~/App_Data/NoodlesDB.sdf");
              //var  configuration =
              //      Fluently
              //          .Configure()
              //          //.Database(MsSqlCeConfiguration.Standard.ConnectionString(@"data source=|DataDirectory|\NoodlesDB.sdf;Password=hemant"))
              //           .Database(MsSqlCeConfiguration.Standard.ShowSql().ConnectionString(dbPath))
              //          .Mappings(m =>
              //          {
              //              m.FluentMappings.AddFromAssemblyOf<UserMaster>();
              //          });


              return Fluently.Configure()
                          .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.Is(@"Data Source=.\SQLExpress;Integrated Security=True;AttachDBFilename=|DataDirectory|NoodlesDB.mdf;User Instance=true")))
                           .Mappings(m =>
                           {
                               //m.FluentMappings.AddFromAssembly(System.Reflection.Assembly.GetExecutingAssembly());
                               ////Scan extensions for nHibernate mappings 
                               //var extension = context.Resolve<IEnumerable<ScriptedExtension>>();
                               //foreach (var assembly in extension.Select(provider => provider.SourceAssembly))
                               //{
                               //    m.FluentMappings.AddFromAssembly(assembly);
                               //}
                                m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly());
                           })     
                          .BuildSessionFactory();
          
           /** For SQL CE 
            *    .Database(MsSqlCeConfiguration.Standard.ShowSql().ConnectionString(c => c.Is(@"data source=|DataDirectory|\NoodlesDB.sdf;Password=hemant")))
            * For SQL Express
            * 
            * 
            * **/
           
            //return configuration.BuildSessionFactory();
        }
    }
}
